//
//  NewFeatureController.swift
//  新浪微博
//
//  Created by 焦林生 on 15/11/9.
//  Copyright © 2015年 焦林生. All rights reserved.
//

import UIKit
import SnapKit

//可重用标示符
private let LJNewFeatureViewCellID = "LJNewFeatureViewCellID"
//新特性图片的数量
private let LJNewFeatureImageCount = 4

class NewFeatureController: UICollectionViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //注册可重用cell
        self.collectionView?.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: LJNewFeatureViewCellID)
       
    }
    
    //隐藏状态栏
    override func prefersStatusBarHidden() -> Bool {
        
        return true
    }
    
    //MARK: -构造函数
    init() {
    //指定的构造函数(流水布局)
        let layout = UICollectionViewFlowLayout()
        //frame的大小取决于layout.itemSize
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        //构造函数 完成之后内部属性才会被创建
       super.init(collectionViewLayout: layout)
        //设置collectionview的属性
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
        
    }

    required init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - 数据源方法
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return LJNewFeatureImageCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(LJNewFeatureViewCellID, forIndexPath: indexPath) as! NewFeatureCell
        
        cell.imageIndex = indexPath.item
        
        return cell
    }
    //scrollView 停止滚动方法
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        //在最后一页才调动动画方法
        //根据contentOffset 计算页数
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        
        //判断是否是最后一页
        if page != LJNewFeatureImageCount - 1
        {
        
            return
        }
        
        //cell播放动画
        let cell = collectionView?.cellForItemAtIndexPath(NSIndexPath(forItem: page, inSection: 0)) as! NewFeatureCell
        
        //显示动画
        cell.showStartButton()
        
    }

   
}



//新特性cell
private class NewFeatureCell: UICollectionViewCell {

    //图像索引
    private var imageIndex: Int = 0 {
    //相当于oc中的重写set方法
        didSet {
        
            iconView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            //隐藏按钮
            startButton.hidden = true
        }
    }
    
    //MARK: -设置界面
    override init(frame: CGRect) {
        super.init(frame: frame)
        print("创建 cell")
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder:) has not been implemented")
    }
    
    //动画显示开始按钮
    private func showStartButton() {
    
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        startButton.userInteractionEnabled = false
        
        UIView.animateWithDuration(1.6,  //动画时常
            delay: 0,       //延迟时间
            usingSpringWithDamping: 0.6, //弹力系数越小越弹
            initialSpringVelocity: 10,    //初始速度 模拟重力加速度
            options: [], //动画选项
            animations: { () -> Void in
                
                self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                
                self.startButton.userInteractionEnabled = true
        }
    }
    
    private func setupUI() {
    
        //1 添加控件(自定义tableviewcell和collectioncviewcell时,都应该将控件加到contentview)
        addSubview(iconView)
        addSubview(startButton)
        
        //不能单纯在此设置隐藏
        startButton.hidden  = true
        
        //2 指定位置
         iconView.frame = bounds
        //设置约束
         startButton.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self.snp_centerX)
            make.bottom.equalTo(self.snp_bottom).multipliedBy(0.7)
            
        }
        
        //3 监听方法
        startButton.addTarget(self, action: "clickStartButton", forControlEvents:.TouchUpInside)
    }
    
    //点击开始体验按钮
    @objc private func clickStartButton() {
    
        print("开始体验")
        
        //发送通知
        NSNotificationCenter.defaultCenter().postNotificationName(LJSwithchRootViewControllerNotification, object: nil)
    }
    
    //MARK:- 懒加载控件
    //图片
    private lazy var iconView: UIImageView = UIImageView()
    
    //开始体验按钮
    private lazy var startButton: UIButton = UIButton(title: "开始体验", color: UIColor.whiteColor(), imageName: "new_feature_finish_button")
    
}









